package com.ruoyi.match.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.match.domain.Tag;
import com.ruoyi.match.domain.Teams;
import com.ruoyi.match.domain.UserTag;
import com.ruoyi.match.domain.vo.TeamsVo;
import com.ruoyi.match.domain.vo.UserInfoVo;
import com.ruoyi.match.service.UserInfoService;
import com.ruoyi.match.service.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/match/userTag")
public class UserTagController extends BaseController {

    @Autowired
    private UserTagService userTagService;

    @Autowired
    private UserInfoService userInfoService;


    // 添加用户标签
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserTag userTag)
    {
        Long userId = SecurityUtils.getUserId();
        userTag.setUserId(userId);
        return toAjax(userTagService.AddUserTag(userTag));
    }

    // 删除用户标签
    @DeleteMapping("/delete/{tagId}")
    public AjaxResult delete(@PathVariable("tagId") Long TagId)
    {
        Long userId = SecurityUtils.getUserId();
        UserTag userTag = new UserTag();
        userTag.setUserId(userId);
        userTag.setTagId(TagId);

        return toAjax(userTagService.deleteUserTag(userTag));
    }



    // 查询用户未选的标签
    @GetMapping("/list/allTag")
    public TableDataInfo listAllTag(Tag tag)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<UserTag> list = userTagService.selectUnChosenTag(userId);
        return getDataTable(list);
    }


    // 查询用户选的标签
    @GetMapping("/list/myTag")
    public TableDataInfo listMyTag()
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<UserTag> list = userTagService.selectMyTag(userId);
        return getDataTable(list);
    }

    // 根据用户名查询用户选的标签
    @GetMapping("/list/Tag/{userName}")
    public TableDataInfo listUserTagById(@PathVariable("userName") String userName)
    {
        Long userId = userInfoService.selectUserId(userName);

        List<UserTag> list = userTagService.selectMyTag(userId);



        return getDataTable(list);
    }

    /**
     * 查询选择了全部指定标签的用户列表
     * @param tagIds 标签 ID 列表
     * @return 用户信息列表
     */
    @PostMapping("/listUsersByTags")
    public AjaxResult listUsersByTags(@RequestBody List<Integer> tagIds) {
        if (tagIds == null || tagIds.isEmpty()) {
            return AjaxResult.error("标签列表不能为空");
        }
        List<UserInfoVo> userList = userTagService.selectUsersByAllTags(tagIds);
        System.out.println(userList);
        return AjaxResult.success(userList);
    }







}
